package com.hanzhong.travel.controller;

import com.hanzhong.travel.entity.Message;
import com.hanzhong.travel.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;
    
    /**
     * 添加消息
     */
    @PostMapping
    public ResponseEntity<?> addMessage(@RequestBody Message message, HttpServletRequest request) {
        // 设置IP地址
        message.setIpAddress(getIpAddress(request));
        
        boolean success = messageService.addMessage(message);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("success", true);
            result.put("message", "感谢您的留言，我们会尽快回复您！");
            return ResponseEntity.ok(result);
        } else {
            result.put("success", false);
            result.put("message", "留言提交失败，请稍后再试");
            return ResponseEntity.badRequest().body(result);
        }
    }
    
    /**
     * 获取所有消息
     */
    @GetMapping
    public ResponseEntity<?> getAllMessages() {
        List<Message> messages = messageService.getAllMessages();
        return ResponseEntity.ok(messages);
    }
    
    /**
     * 分页获取消息
     */
    @GetMapping("/page")
    public ResponseEntity<?> getMessagesByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = messageService.getMessagesByPage(page, size);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 根据ID获取消息
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getMessageById(@PathVariable Long id) {
        Message message = messageService.getMessageById(id);
        if (message != null) {
            return ResponseEntity.ok(message);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/{id}/read")
    public ResponseEntity<?> markAsRead(@PathVariable Long id) {
        boolean success = messageService.markAsRead(id);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("success", true);
            result.put("message", "消息已标记为已读");
            return ResponseEntity.ok(result);
        } else {
            result.put("success", false);
            result.put("message", "操作失败，消息不存在");
            return ResponseEntity.badRequest().body(result);
        }
    }
    
    /**
     * 删除消息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteMessage(@PathVariable Long id) {
        boolean success = messageService.deleteMessage(id);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("success", true);
            result.put("message", "消息已删除");
            return ResponseEntity.ok(result);
        } else {
            result.put("success", false);
            result.put("message", "删除失败，消息不存在");
            return ResponseEntity.badRequest().body(result);
        }
    }
    
    /**
     * 获取客户端IP地址
     */
    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
} 